package com.csh.szhm.activity;

import com.android.volley.VolleyError;
import com.csh.szhm.R;
import com.csh.szhm.custorm.CustomProgressDialog;
import com.csh.szhm.custorm.CustomProgressDialog.onCencalProGress;
import com.csh.szhm.http.HttpCallBackUi;
import com.csh.szhm.http.HttpRequestTool;
import com.csh.szhm.utils.ActivityStartAndFinshAnimation;
import com.csh.szhm.utils.ActivityTack;
import com.google.gson.reflect.TypeToken;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;

public abstract class MyBaseActivity<T> extends FragmentActivity implements
		HttpCallBackUi<T> {

	protected int pageIndex = 1;
	protected int pageSize = 20;
	protected boolean isRefresh;
	protected HttpRequestTool<T> mHttpRequestTool;
	protected ActivityTack tack = ActivityTack.getInstanse();
	protected TypeToken<T> mTypeToken;
	protected String mRequestType;
	private TextView mTitleText;
	private TextView mTitleRight;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		init();
	}

	/**
	 * 初始化空间
	 */
	protected abstract void init();

	protected void initContentView(int view) {
		setContentView(view);
	}

	public void click(View v) {
		if (v.getId() == R.id.szhm_title_back_root) {
			// 页面返回
			finish();
		}
	}

	protected void setMyTitle(Integer... integers) {
		if (integers != null) {
			mTitleText = (TextView) findViewById(R.id.szhm_title_txt_id);
			mTitleRight = (TextView) findViewById(R.id.szhm_title_right_id);
			if (integers.length > 0) {
				mTitleText.setText(integers[0]);
			}
			if (integers.length > 1) {
				mTitleRight.setBackgroundResource(integers[1]);
			}

		}
	}

	protected void setMyTitle(String title) {
		mTitleText = (TextView) findViewById(R.id.szhm_title_txt_id);
		// mTitleRight = (TextView) findViewById(R.id.szhm_title_right_id);
		mTitleText.setText(title);
	}

	protected void requestNet() {
		shouCustomProgressDialog();
	}

	private CustomProgressDialog mCustomProgressDialog;

	public void shouCustomProgressDialog() {
		if (mCustomProgressDialog == null) {
			mCustomProgressDialog = CustomProgressDialog.createDialog(this);
			mCustomProgressDialog.setOnCancelProGress(new onCencalProGress() {
				@Override
				public void dissmis() {
					dissmisCallChild();
				}
			});
		}
		mCustomProgressDialog.show();
	}

	protected void dissmisCallChild() {
		finish();
	}

	public void dissmisCustomProgressDialog() {
		if (mCustomProgressDialog != null && mCustomProgressDialog.isShowing()) {
			mCustomProgressDialog.dismiss();
		}
	}

	public void startMyActivity(Class cls) {
		Intent intent = new Intent();
		intent.setClass(this, cls);
		startActivity(intent);
		ActivityStartAndFinshAnimation.ActivityStartAnimation(this);
	}

	public void startMyActivity(Intent in, Class cls) {
		in.setClass(this, cls);
		startActivity(in);
		ActivityStartAndFinshAnimation.ActivityStartAnimation(this);
	}

	@Override
	public void finish() {
		super.finish();
		ActivityStartAndFinshAnimation.ActivityFinish(this);
	}

	@Override
	public void success(T data) {
		// TODO Auto-generated method stub
		dissmisCustomProgressDialog();

	}

	@Override
	public void failed(VolleyError error) {
		// TODO Auto-generated method stub
		dissmisCustomProgressDialog();
	}

	@Override
	public void requestType(String Type) {
		mRequestType = Type;

	}
}
